/**
 * 
 */
package com.smarthome.core.message.impl;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.smarthome.base.service.ShSmsRecordService;
import com.smarthome.base.service.ShSmsTempletService;
import com.smarthome.common.exception.ExceptionUtils;
import com.smarthome.core.message.MessageService;
import com.smarthome.domain.bean.ShSmsRecord;
import com.smarthome.domain.bean.ShSmsTemplet;
import com.smarthome.domain.enums.SmsExceptionEnum;
import com.smarthome.domain.enums.SmsStatusEnum;
import com.smarthome.domain.enums.SmsTypeEnum;
import com.smarthome.rpc.SmsService;

/**
 * @author dhc
 * @date 2015年10月29日 下午11:37:25
 * @title
 */
@Service("messageDefaultServiceImpl")
public class MessageDefaultServiceImpl implements MessageService{
	@Resource
	protected ShSmsRecordService recordService;

	@Resource(name = "smsService")
	protected SmsService smsService;

	@Resource(name = "shSmsTempletService")
	protected ShSmsTempletService templetService;
	
	protected SmsTypeEnum getCaptchaType(){
		return SmsTypeEnum.COMMON_CAPTCHA;
	}
	
	
	@Override
	public ShSmsRecord send(ShSmsRecord record) {
		if (null == record)
			ExceptionUtils.throwSms(SmsExceptionEnum.NULL);
			//record.setContent(getContent(record));
			record.setStatus(SmsStatusEnum.SEDING.getCode());
			record.setIsValidate(0);
			smsService.sendSms(record);
	/*	try {
		} catch (SmsException e) {
			record.setStatus(SmsStatusEnum.FAIL.getCode());
			record.setErrorMsg(e.getMessage());
			record.setErrorDate(new Date());
		}*/
		recordService.insert(record);

		return record;
	}

	@Override
	public boolean validateCaptcha(String captcha,String urid) {
		if(StringUtils.isBlank(urid)) ExceptionUtils.throwSms(SmsExceptionEnum.URID_ISNULL);
		if(StringUtils.isBlank(captcha)) ExceptionUtils.throwSms(SmsExceptionEnum.CAPTCHA_ISNULL);
		ShSmsRecord record = new ShSmsRecord();
		record.setUrid(urid);
		record.setIsValidate(0);
		record.setOrderField("id");
		record.setOrderFieldType("DESC");
		List<ShSmsRecord> records = recordService.selectEntryList(record);
		if(null != records && records.size() > 0){
			SmsTypeEnum type = getCaptchaType();
			ShSmsRecord saveRecord = records.get(0);
			if(null == captcha || !captcha.equals(saveRecord.getCaptcha())) return false;
			
			if(null != type.getOverTime()  && type.getOverTime() .longValue() > 0 && null !=  saveRecord.getGmtCreate()){
				//比较时间
				long eq = saveRecord.getGmtCreate().getTime() + type.getOverTime()*1000;
				if(eq < System.currentTimeMillis()){
					ExceptionUtils.throwSms(SmsExceptionEnum.CAPTCHA_OVERTIEM);
				}
			}
			saveRecord.setIsValidate(1);
			recordService.saveOrUpdate(saveRecord);
			return true;
		}
		return false;
	}
	
	protected String getContent(ShSmsRecord record) {
		if (null == record)
			return "";
		String content = record.getContent();
		// 查询模板
		if (null != record.getTypeId() && record.isTemplet()) {
			ShSmsTemplet templet = new ShSmsTemplet();
			templet.setTypeId(record.getTypeId());
			templet.setIsUsed(1);
			List<ShSmsTemplet> ts = templetService.selectEntryList(templet);
			if (null != ts && ts.size() > 0) {
				ShSmsTemplet t = ts.get(0);
				String temp = t.getContent();
				if (StringUtils.isEmpty(temp)) {
					ExceptionUtils.throwSms(SmsExceptionEnum.TEMPLET_CONTENTNULL);
				}
				Map<String, String> stemp = record.getTempletContent();

				Pattern pattern = Pattern.compile("\\{[a-z]*[A-Z]*\\}");
				Matcher matcher = pattern.matcher(temp);

				while (matcher.find()) {
					String group = matcher.group();
					String key = group.replaceAll("\\{|\\}", "");
					String tip = stemp.get(key);
					if (null == tip)
						tip = "";

					temp = temp.replace(group, tip);
				}

				content = temp;
			} else {
				ExceptionUtils.throwSms(SmsExceptionEnum.TEMPLET_NOTFIND);
			}
		}

		return content;
	}
}
